/**
 * @description 基于TCP聊天例程--服务端程序部分
 *
 * 
 * @pageNo 449
 * @author mlmoria
 * @date 2017年5月2日
 * 
 */

package No12Net.Chart.Demo;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class J_ChartServer extends JFrame{

	
	private ObjectInputStream m_input;//输入流
	private ObjectOutputStream m_output;//输出流 
	private JTextField m_enter;//输入区域
	private JTextArea m_display;//显示区域
	private int m_clientNumber=0;//连接的客户数
	
	public J_ChartServer(){
		super("聊天程序服务器端");
		Container c=getContentPane();
		m_enter=new JTextField();
		m_enter.setEnabled(false);
		m_enter.addActionListener(new ActionListener(){			

			@Override
			public void actionPerformed(ActionEvent event) {
			   String s=event.getActionCommand();
			   try {
				m_output.writeObject(s);
				m_output.flush();
				mb_displayAppend("服务端："+s);
				m_enter.setText("");//清空输入区域原有内容
			} catch (IOException e) {
				System.err.println("发生异常："+e);
				e.printStackTrace();
			}		
			}
			
		});
		
		c.add(m_enter,BorderLayout.NORTH);
		m_display=new JTextArea();
		c.add(new JScrollPane(m_display),BorderLayout.CENTER);   
	}//J_ChartServer构造方法结束
	
	public void mb_displayAppend(String s){
		m_display.append(s+"\n");
		m_display.setCaretPosition(m_display.getText().length());//将光标位置放置到str末尾
		
		m_enter.requestFocusInWindow();//转移输入焦点到输入区域
		
	}//方法mb_displayAppend结束
	
	public boolean mb_isEndSesion(String m){
		if(m.equalsIgnoreCase("q"))return(true);
		if(m.equalsIgnoreCase("quit"))return(true);
		if(m.equalsIgnoreCase("exit"))return(true);
		if(m.equalsIgnoreCase("end"))return(true);
		if(m.equalsIgnoreCase("结束"))return(true);
		return(false);
	}
	
	
	public void mb_run(){
		try {
			
			ServerSocket server =new ServerSocket(5000);
			String m;
			while(true){
				m_clientNumber++;
				mb_displayAppend("等待连接【"+m_clientNumber+"】");
				Socket s=server.accept();
				mb_displayAppend("收到客户端连接【"+m_clientNumber+"】");
				m_output=new ObjectOutputStream(s.getOutputStream());
				m_input=new ObjectInputStream(s.getInputStream());
				m_output.writeObject("连接成功");
				m_output.flush();
				m_enter.setEnabled(true);
				do{
					m=(String) m_input.readObject();
					mb_displayAppend("客户端"+m);
				}while(!mb_isEndSesion("服务端"+m));//do while 结束
				m_output.writeObject("q");
				m_output.flush();
				m_enter.setEnabled(false);
				m_output.close();
				m_input.close();
				s.close();
				mb_displayAppend("连接【"+m_clientNumber+"】结束");
			}//while结束
			
		} catch (Exception e) {
			System.err.println("发生异常："+e);
			e.printStackTrace();
			mb_displayAppend("连接【"+m_clientNumber+"】发生异常");
		}
		
	}
	
	public static void main(String[] args) {
		J_ChartServer app=new J_ChartServer();
		app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		app.setSize(350,150);
		app.setVisible(true);
		app.mb_run();
		
	}//main结束
	
}
